package com.shaoshaohuo.app.view;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.HomeCouponEntity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.ui.LoginActivity;
import com.shaoshaohuo.app.utils.LoginUtils;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/8/17.
 */

public class CouPons extends Dialog implements View.OnClickListener {

    private LinearLayout lin_iv_yhj;
    private LinearLayout lin_kuangkuang;
    private ImageView iv_click_lq;
    private ImageView iv_bk_yezi;
    private ImageView iv_lin_bk;
    private ImageView lhbl;
    private Handler handler = new Handler();

    public CouPons(@NonNull Context context) {
        super(context, R.style.coudiast);
        init();
    }

    private void init() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.dialog, null);
        setContentView(inflate);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.dimAmount = 0.8f;
        attributes.alpha = 1f;
        attributes.height = getContext().getResources().getDisplayMetrics().heightPixels;
        attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
        setCanceledOnTouchOutside(true);
        attributes.gravity = Gravity.BOTTOM;

        initview(inflate);

    }

    private void initview(View inflate) {
        lin_kuangkuang = (LinearLayout) inflate.findViewById(R.id.lin_kuangkuang);
        ViewGroup.LayoutParams layoutParams = lin_kuangkuang.getLayoutParams();
        layoutParams.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * (3f / 4f));
        layoutParams.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels / 2 - getstarbarheight());
        View viewById = inflate.findViewById(R.id.lin_kuangkuang_1);
        ViewGroup.LayoutParams viewByIdsss = viewById.getLayoutParams();
        viewByIdsss.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * (3f / 4f));
        viewByIdsss.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels / 2 - getstarbarheight());
        inflate.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //领取按钮
        iv_click_lq = (ImageView) inflate.findViewById(R.id.iv_click_lq);
        iv_click_lq.setOnClickListener(this);
        lin_iv_yhj = (LinearLayout) inflate.findViewById(R.id.lin_iv_yhj);
        //背景叶子
        iv_bk_yezi = (ImageView) inflate.findViewById(R.id.iv_bk_yezi);
        //背景红包
        iv_lin_bk = (ImageView) inflate.findViewById(R.id.iv_lin_bk);
        iv_lin_bk.setScaleType(ImageView.ScaleType.FIT_XY);
        ViewGroup.LayoutParams iv_lin_bksss = iv_lin_bk.getLayoutParams();
        iv_lin_bksss.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * (3f / 4f));
        iv_lin_bksss.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels / 2 - getstarbarheight());
        lhbl = (ImageView) inflate.findViewById(R.id.lhbl);

    }

    //设置图片的方法
    public void setpica(HomeCouponEntity homeCouponEntity) {
        HomeCouponEntity.DataBeanXX.DataBeanX.ImgMapBean imgMap = homeCouponEntity.getData().getData().getImgMap();
        String a = imgMap.getA();
        String b = imgMap.getB();
        String c = imgMap.getC();
        String d = imgMap.getD();
        Picasso.with(getContext()).load(a).into(iv_bk_yezi);
        Picasso.with(getContext()).load(b).into(iv_lin_bk);
        Picasso.with(getContext()).load(c).into(lhbl);
        Picasso.with(getContext()).load(d).into(iv_click_lq);
    }


    public void addyhj(List<View> views) {
        lin_iv_yhj.removeAllViews();
        for (int i = 0; i < views.size(); i++) {
            lin_iv_yhj.addView(views.get(i));
          /*
          *    layoutParams.width= (int) (getContext().getResources().getDisplayMetrics().widthPixels*(3f/4f));
        layoutParams.height=getContext().getResources().getDisplayMetrics().heightPixels/2;*/
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) views.get(i).getLayoutParams();
            layoutParams.width = (int) (0.9 * (int) (getContext().getResources().getDisplayMetrics().widthPixels * (3f / 4f)));
            layoutParams.height = (int) ((getContext().getResources().getDisplayMetrics().heightPixels / 2 - getstarbarheight()) * 1f / 5f - (int) getContext().getResources().getDimension(R.dimen.activity_horizontal_margin));
            layoutParams.setMargins(0, (int) getContext().getResources().getDimension(R.dimen.activity_horizontal_margin), 0, 0);
            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        }


    }

    public void dismissdialog() {
//      new ScaleAnimation()
//      new RotateAnimation()
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(lin_kuangkuang, "rotationY", 0, 360);
        scaleY.setDuration(200).setRepeatCount(2);
        scaleY.setRepeatMode(ValueAnimator.RESTART);
        scaleY.setInterpolator(new LinearInterpolator());
        scaleY.start();
    }

    public float getstarbarheight() {
        try {
            Class<?> aClass = Class.forName("android.R.dimen.status_bar_height");
            Object o = aClass.newInstance();
            String status_bar_height = aClass.getField("status_bar_height").get(0).toString();
            int i = Integer.parseInt(status_bar_height);
            float dimension = getContext().getResources().getDimension(i);
            return dimension;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public String activityid = "";

    @Override
    public void onClick(View v) {
        if (LoginUtils.IsLogining()) {
            dismiss();
            if (activityid.length() > 0) {
                Map<String, String> headerParams = RequestParam.getHeaderParams();
                ///Coupon/GetCoupon
                HashMap<String, String> stringStringHashMap = new HashMap<>();
                stringStringHashMap.put("activityid", activityid);
                Okhttputils.Instanse(getContext()).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + "/Coupon/GetCoupon", stringStringHashMap, new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("请检查您的网络!");
                            }
                        });
                    }

                    @Override
                    public void error(Call call, int code) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("领取失败!");
                            }
                        });
                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("领取成功!");
                            }
                        });
                    }
                }, BaseEntity.class);
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.toast("领取失败!");
                    }
                });
            }
        } else {
            Intent intent = new Intent();
            intent.setClass(getContext(), LoginActivity.class);
            getContext().startActivity(intent);
        }
    }

}
